#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

// 常用查找算法  count_if

// 统计内置数据类型
class Greater20
{
public:
    bool operator()(int val)
    {
        return val > 20;
    }
};

void test1()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    v.push_back(30);

    int num = count_if(v.begin(), v.end(), Greater20());

    cout << "大于20的元素个数为：" << num << endl;
}

// 统计自定义数据类型
class Person
{
public:
    string name;
    int age;

    Person(string name, int age)
    {
        this->name = name;
        this->age = age;
    }
};

class Greater200
{
public:
    bool operator()(const Person& p)
    {
        return p.age > 200;
    }
};

void test2()
{
    vector<Person>v;

    // 创建数据
    Person p1("孙悟空", 999);
    Person p2("唐僧", 35);
    Person p3("猪八戒", 800);
    Person p4("沙僧", 35);
    Person p5("小白龙", 1000);

    // 放入到容器中
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    // 统计 大于200岁人员个数
    int num = count_if(v.begin(), v.end(), Greater200());

    cout << "大于200岁的人员个数为：" << num << endl;
}

int main()
{
    test1();
    cout << "------------------------" << endl;
    test2();
    
    return 0;
}